package com.ptszyxx.popose.module.main.user.vm;

import android.app.Application;

import androidx.annotation.NonNull;

import com.ptszyxx.popose.R;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseListResponse;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessListResult;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.http.data.entity.user.UserVoiceResult;
import com.ysg.http.data.entity.user.VoiceRandomSignEntity;
import com.ysg.utils.YToastUtil;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class UserEditVoiceVM extends BaseViewModel<CommonRepository> {

    public UserEditVoiceVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onVoiceSignEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onStartEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onRestartEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onListenerEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onSubmitEvent = new SingleLiveEvent<>();
    }

    public BindingCommand onRandomCommand = new BindingCommand(()-> requestVoiceRandom());
    public BindingCommand onStartCommand = new BindingCommand(()-> uc.onStartEvent.call());
    public BindingCommand onRestartCommand = new BindingCommand(()-> uc.onRestartEvent.call());
    public BindingCommand onListenerCommand = new BindingCommand(()-> uc.onListenerEvent.call());
    public BindingCommand onSubmitCommand = new BindingCommand(()-> uc.onSubmitEvent.call());

    /**
     *
     */
    public void requestVoiceRandom(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .data(model.voiceRandomSign(map), this,
                        new OnSuccessResult<VoiceRandomSignEntity>() {
                            @Override
                            public void onSuccessResult(BaseResponse<VoiceRandomSignEntity> response) {
                                uc.onVoiceSignEvent.setValue(response.getData().getContent());
                            }
                        });
    }
    /**
     *
     */
    public void requestUploadAudio(File file, String times){
        HttpUtils.getInstance()
                .list(model.uploadFile(file), this,
                        new OnSuccessListResult<UserVoiceResult>() {
                            @Override
                            public void onSuccessResult(BaseListResponse<UserVoiceResult> response) {
                                requestVoiceSignature(response.getData().get(0).getName(), times);
                            }
                        });

    }

    /**
     *
     */
    public void requestVoiceSignature(String url, String times){
        Map<String, String> map = new HashMap<>();
        map.put("voiceUrl", url);
        map.put("seconds", times);
        HttpUtils.getInstance()
                .data(model.voiceSignature(map), this,
                        new OnSuccessResult<UserVoiceResult>() {
                            @Override
                            public void onSuccessResult(BaseResponse<UserVoiceResult> response) {
                                YToastUtil.showShort(R.string.success);
                                finish();
                            }
                        });
    }



}
